<template>
  <u-image width="100vw" height="100vh" src="https://www.wantongkonggu.cn/crmebimage/profile/upload/2025/04/23/wy_20250423173637A001.png" mode="aspectFill" @click="goLink"> </u-image>
</template>

<script>
import { getUserInfo, programlogin } from "../../utils/request_api.js"; /* 登录界面挪动接口 */
export default {
  data() {
    return {
      userInfo: "",
    };
  },

  onLoad() {
    /* this.logggggin(); */
  },
  methods: {
    async goLink() {
      uni.navigateTo({
        url: `/pages_h5/wy/index_list/comunity_integral`,
      });
    },
    async logggggin() {
      var e = await getUserInfo();
      if (e.code == 401) {
        this.showloginJs();
      } else {
        this.userInfo = e.data;

        uni.setStorageSync("userInfo", this.userInfo);
        uni.hideLoading();
      }
    },
    initUser() {
      getUserInfo()
        .then((res) => {
          if (res.code === 401) {
            uni.showToast({
              title: "您的登录已过期，请重新登录",
              icon: "none",
            });
            uni.clearStorage();
            this.showloginJs();
            // return;
          } else {
            this.userInfo = res.data;
            uni.setStorageSync("userInfo", res.data);
            uni.hideLoading();
          }
        })
        .catch((err) => {
          console.error("获取用户信息失败", err);
        });
    },
    showloginJs() {
      let that = this;
      uni.login({
        provider: "weixin",
        success: (res) => {
          if (res.code) {
            that.autoLogin(res.code);
          } else {
            uni.hideLoading();
            console.error("登录失败:", res.errMsg);
            uni.showToast({
              title: "微信登录失败，请稍后再试",
              icon: "none",
            });
          }
        },
        fail: (err) => {
          uni.hideLoading();
          console.error("登录失败:", err);
          uni.showToast({
            title: "微信登录失败，请稍后再试",
            icon: "none",
          });
        },
      });
      // let storedOpenID = uni.getStorageSync("openID");
      // if (storedOpenID) {
      //   this.authKey = storedOpenID;
      //   this.initUser();
      // } else {

      // }
    },
    /* 免登录-每个界面调用 */
    async autoLogin(code) {
      let that = this;
      wx.getUserInfo({
        success(res) {
          const { nickName, avatarUrl, gender } = res.userInfo;
          programlogin({ code, avatar: avatarUrl, nickName, sex: gender }).then((response) => {
            const data = response.data;
            that.authKey = data.key;
            if (data.type === "login") {
              uni.setStorageSync("uid", data.uid);
              that.ebUserId = uni.getStorageSync("uid");
              uni.setStorageSync("token", data.token);
              uni.setStorageSync("userInfo_data", data);
              uni.setStorageSync("openID", data.key);
              that.initUser();
            } else {
              uni.showModal({
                title: "提示",
                content: "您的登录已过期或未登录，请重新登录。",
                success: (res) => {
                  if (res.confirm) {
                    that.showzhezhao = false;
                    uni.hideLoading();
                    uni.clearStorage();
                    uni.switchTab({
                      url: "/pages_h5/myCenter/index", // 假设登录页面的路径为 /pages_h5/login/index
                    });
                  }
                },
                fail: () => {
                  that.showzhezhao = false;
                  uni.hideLoading();
                },
              });
            }
          });
        },
        fail: () => {
          uni.showToast({
            title: "您需要授权，才能获取您的信息！",
          });
        },
      });
    },
  },
};
</script>

<style>
/* 如果你需要额外的样式，可以在这里添加 */
</style>
